# @Version        : 1.0
# @Update Time    : 2025/3/14 22:27
# @File           : order_management_view.py
# @IDE            : PyCharm
# @Desc           : 文件描述信息

import flet as ft


class OrderCard(ft.Container):
    def __init__(
        self,
        order_date: str,
        amount: float,
        status: str,
        delivery_address: str,
        product_image: str,
        **kwargs
    ):
        super().__init__(
            padding=15,
            bgcolor="white",
            border_radius=10,
            shadow=ft.BoxShadow(
                spread_radius=1,
                blur_radius=5,
                color=ft.colors.GREY_300,
                offset=ft.Offset(0, 2)
            ),
            **kwargs
        )

        # 状态样式配置
        status_config = {
            "配送中": {"color": "#4CAF50", "icon": ft.icons.DELIVERY_DINING},
            "已送达": {"color": "#9E9E9E", "icon": ft.icons.CHECK_CIRCLE_OUTLINE}
        }

        self.content = ft.Row(
            controls=[
                ft.Image(src=product_image, width=60, height=60, border_radius=8),
                ft.Column(
                    controls=[
                        ft.Text(f"{order_date}订单", size=14, weight=ft.FontWeight.BOLD),
                        ft.Text(f"￥{amount}", color="#2196F3", size=16),
                        ft.Text(delivery_address, color="grey", size=12),
                    ],
                    expand=True,
                    spacing=6
                ),
                ft.Icon(
                    name=status_config[status]["icon"],
                    color=status_config[status]["color"],
                    size=30
                )
            ],
            spacing=15,
            vertical_alignment=ft.CrossAxisAlignment.CENTER
        )


class OrderView(ft.Column):
    def __init__(self, page: ft.Page, params: dict = None):
        super().__init__(spacing=20)
        self.page = page
        self.params = params
        self.orders = [
            {"order_date": "2024/03/21", "amount": 16.8, "status": "配送中", "delivery_address": "杭州市西湖区",
             "product_image": "assets/mango.png"},
            {"order_date": "2024/03/21", "amount": 20.8, "status": "配送中", "delivery_address": "杭州市西湖区",
             "product_image": "assets/grapes.png"},
            {"order_date": "2024/02/13", "amount": 28.6, "status": "已送达", "delivery_address": "上海市浦东新区",
             "product_image": "assets/grape.png"},
            {"order_date": "2024/02/12", "amount": 16.9, "status": "已送达", "delivery_address": "北京市朝阳区",
             "product_image": "assets/strawberry.png"},
        ]
        self.filtered_orders = self.orders
        self._build_ui()

    def _build_header(self):
        return ft.Column(
            controls=[
                ft.Text("订单", size=20, weight=ft.FontWeight.BOLD),
                ft.TextField(
                    label="搜索你的订单",
                    border_color="#BDBDBD",
                    filled=True,
                    bgcolor="#FAFAFA",
                    suffix_icon=ft.icons.SEARCH,
                    border_radius=10
                )
            ],
            spacing=15
        )

    def _build_filter_buttons(self):
        def filter_orders(e):
            filter_status = e.control.text
            if filter_status == "全部":
                self.filtered_orders = self.orders
            else:
                self.filtered_orders = [order for order in self.orders if order["status"] == filter_status]
            self._build_tabs()

        return ft.Row(
            controls=[
                ft.FilledTonalButton(
                    "全部",
                    style=ft.ButtonStyle(color="#4CAF50"),
                    width=100,
                    on_click=filter_orders
                ),
                ft.FilledTonalButton(
                    "配送中",
                    style=ft.ButtonStyle(color="#4CAF50"),
                    width=100,
                    on_click=filter_orders
                ),
                ft.FilledTonalButton(
                    "已送达",
                    style=ft.ButtonStyle(color="#9E9E9E"),
                    width=100,
                    on_click=filter_orders
                ),
                ft.FilledTonalButton(
                    "已取消",
                    style=ft.ButtonStyle(color="#9E9E9E"),
                    width=100,
                    on_click=filter_orders
                )
            ],
            spacing=10
        )

    def _build_tabs(self):
        current_orders = [order for order in self.filtered_orders if order["order_date"] >= "2024/03/01"]
        historical_orders = [order for order in self.filtered_orders if order["order_date"] < "2024/03/01"]
        chart = ft.BarChart(
            bar_groups=[
                ft.BarChartGroup(
                    x=0,
                    bar_rods=[
                        ft.BarChartRod(
                            from_y=0,
                            to_y=40,
                            width=30,
                            color=ft.colors.AMBER,
                            tooltip="40",
                            border_radius=0,
                        ),
                    ],
                ),
                ft.BarChartGroup(
                    x=1,
                    bar_rods=[
                        ft.BarChartRod(
                            from_y=0,
                            to_y=100,
                            width=30,
                            color=ft.colors.BLUE,
                            tooltip="100",
                            border_radius=0,
                        ),
                    ],
                ),
                ft.BarChartGroup(
                    x=2,
                    bar_rods=[
                        ft.BarChartRod(
                            from_y=0,
                            to_y=30,
                            width=30,
                            color=ft.colors.RED,
                            tooltip="30",
                            border_radius=0,
                        ),
                    ],
                ),
                ft.BarChartGroup(
                    x=3,
                    bar_rods=[
                        ft.BarChartRod(
                            from_y=0,
                            to_y=60,
                            width=30,
                            color=ft.colors.ORANGE,
                            tooltip="60",
                            border_radius=0,
                        ),
                    ],
                ),
            ],
            border=ft.border.all(1, ft.colors.GREY_400),
            left_axis=ft.ChartAxis(
                labels_size=40, title=ft.Text("2025年每月销售金额"), title_size=40
            ),
            bottom_axis=ft.ChartAxis(
                labels=[
                    ft.ChartAxisLabel(
                        value=0, label=ft.Container(ft.Text("2025-01"), padding=5)
                    ),
                    ft.ChartAxisLabel(
                        value=1, label=ft.Container(ft.Text("2025-02"), padding=5)
                    ),
                    ft.ChartAxisLabel(
                        value=2, label=ft.Container(ft.Text("2025-03"), padding=5)
                    ),
                    ft.ChartAxisLabel(
                        value=3, label=ft.Container(ft.Text("2025-04"), padding=5)
                    ),
                ],
                labels_size=40,
            ),
            horizontal_grid_lines=ft.ChartGridLines(
                color=ft.colors.GREY_300, width=1, dash_pattern=[3, 3]
            ),
            tooltip_bgcolor=ft.colors.with_opacity(0.5, ft.colors.GREY_300),
            max_y=110,
            interactive=True,
            expand=True,
        )
        # 添加统计图 Tab
        stats_tab = ft.Tab(
            text="统计",
            content=ft.Container(
                chart,
                padding=ft.Padding(1, 20, 1, 1),
                border=ft.border.all(1, ft.colors.GREY_400),
            )
        )

        return ft.Tabs(
            tabs=[
                ft.Tab(
                    text="当前",
                    content=ft.ListView(
                        controls=[OrderCard(**order) for order in current_orders],
                        spacing=10,
                        expand=True
                    )
                ),
                ft.Tab(
                    text="历史",
                    content=ft.ListView(
                        controls=[OrderCard(**order) for order in historical_orders],
                        spacing=10,
                        expand=True
                    )
                ),
                stats_tab  # 添加统计图 Tab
            ],
            expand=True
        )

    def _build_ui(self):
        self.controls = [
            self._build_header(),
            self._build_filter_buttons(),
            ft.Divider(height=20, color=ft.colors.TRANSPARENT),
            self._build_tabs()
        ]
